package com.fin.zw.intelligentbot.service;

import com.fin.zw.intelligentbot.entity.AiAccount;
import com.fin.zw.intelligentbot.entity.AiConfiguration;
import com.fin.zw.intelligentbot.entity.Company;
import com.fin.zw.intelligentbot.entity.QAiConfiguration;
import com.fin.zw.intelligentbot.enums.SourceType;
import com.fin.zw.intelligentbot.enums.VoiceType;
import com.fin.zw.intelligentbot.model.request.AiConfigRequest;
import com.fin.zw.intelligentbot.model.session.SessionModel;
import com.fin.zw.intelligentbot.repository.AiAccountRepository;
import com.fin.zw.intelligentbot.repository.AiConfigurationRepository;
import com.google.common.collect.Lists;
import com.querydsl.core.BooleanBuilder;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Set;

@Service("aiConfigurationService")
public class AiConfigurationService {

    private static final Logger log = LoggerFactory.getLogger(AiConfigurationService.class);

    @Autowired
    private AiConfigurationRepository aiConfigurationRepository;

    @Autowired
    private AiAccountRepository aiAccountRepository;

    @Autowired
    private CommonService commonService;

    /**
     *  分页查询ai语音配置
     * @param pageable
     * @param sessionModel
     * @param name
     * @return
     */
    public Page<AiConfiguration> queryAiConfiguration(Pageable pageable,
                                                      SessionModel sessionModel,
                                                      String name, VoiceType type) {
        BooleanBuilder builder = new BooleanBuilder();
        QAiConfiguration qAiConfiguration = QAiConfiguration.aiConfiguration;
        if(sessionModel.getCompanyId() == null){
            builder.and(qAiConfiguration.source.eq(SourceType.SAFEMANAGER));
        }else{
            Company company = commonService.getCompany(sessionModel);
            builder.and(qAiConfiguration.companies.contains(company));
        }
        if(StringUtils.isNoneBlank(name)){
            builder.and(qAiConfiguration.name.eq(name));
        }
        if (type != null) {
            builder.and(qAiConfiguration.type.eq(type));
        }
        return aiConfigurationRepository.findAll(builder,pageable);
    }


    /**
     * 查询ai配置，不分页
     *
     * @param sessionModel
     * @param type
     * @return
     */
    public List<AiConfiguration> getAiConfigurationList(SessionModel sessionModel, VoiceType type) {
        BooleanBuilder builder = new BooleanBuilder();
        QAiConfiguration qAiConfiguration = QAiConfiguration.aiConfiguration;
        if (sessionModel.getCompanyId() == null) {
            builder.and(qAiConfiguration.source.eq(SourceType.SAFEMANAGER));
        } else {
            Company company = commonService.getCompany(sessionModel);
            builder.and(qAiConfiguration.companies.contains(company));
        }
        if (type != null) {
            builder.and(qAiConfiguration.type.eq(type));
        }
        Iterator<AiConfiguration> iterator = aiConfigurationRepository.findAll(builder).iterator();
        return Lists.newArrayList(iterator);
    }

    /**
     * ai语音配置信息添加
     * @param request
     * @param sessionModel
     */
    public void createAiConfig(AiConfigRequest request,SessionModel sessionModel){
        AiConfiguration aiConfiguration = new AiConfiguration();
        if(sessionModel.getCompanyId() == null){
            aiConfiguration.setSource(SourceType.SAFEMANAGER);
        }else{
            aiConfiguration.setSource(SourceType.COMPANY);
        }
        Set<Company> set = commonService.getCompanySet(sessionModel,request.getCompanyIds());
        aiConfiguration.setCompanies(set);
        aiConfiguration.setType(request.getType());
        aiConfiguration.setParam(request.getParam());
        aiConfiguration.setName(request.getName());
        aiConfiguration.setMader(request.getMader());
        Optional<AiAccount> aiAccountOptional = aiAccountRepository.findById(request.getAccountId());
        if(!aiAccountOptional.isPresent()){
            log.error("选择的ai账号信息不存在");
        }
        aiConfiguration.setAccounts(aiAccountOptional.get());
        aiConfigurationRepository.save(aiConfiguration);
    }

    /**
     * 语音配置修改
     * @param request
     * @param sessionModel
     */
    public void updateAiConfig(AiConfigRequest request,SessionModel sessionModel){
        Optional<AiConfiguration> aiConfigurationOptional = aiConfigurationRepository.findById(request.getId());
        if(!aiConfigurationOptional.isPresent()){
            log.error("该配置信息不存在");
        }
        AiConfiguration aiConfiguration = aiConfigurationOptional.get();
        Set<Company> set = commonService.getCompanySet(sessionModel,request.getCompanyIds());
        aiConfiguration.setCompanies(set);
        aiConfiguration.setType(request.getType());
        aiConfiguration.setParam(request.getParam());
        aiConfiguration.setName(request.getName());
        aiConfiguration.setMader(request.getMader());
        Optional<AiAccount> aiAccountOptional = aiAccountRepository.findById(request.getAccountId());
        if(!aiAccountOptional.isPresent()){
            log.error("选择的ai账号信息不存在");
        }
        aiConfiguration.setAccounts(aiAccountOptional.get());
        aiConfigurationRepository.save(aiConfiguration);
    }
}
